<?php
namespace Admin\Controller;
use Think\Controller;
class StatisticsController extends CommonController {
    public function index(){
		
		$this->display();
    }
	/**
	* @var date1日期1 
	* @var date2 日期2 
	* @var tags 年月日之间的分隔符标记,默认为'-'  
	* @return 相差的月份数量 
	* @example: $date1 = "2003-08-11"; $date2 = "2008-11-06"; $monthNum = getMonthNum( $date1 , $date2 ); echo $monthNum; 
	*/
	function getMonthNum( $date1, $date2, $tags='-' ){  
		$date1 = explode($tags,$date1);  
		$date2 = explode($tags,$date2);
		return abs(($date2[0] - $date1[0]) * 12 + ($date2[1] - $date1[1])); 
	}

	//订单统计
	public function order(){
		$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));

		$param = I('param.','','trim');
		
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		
		$goods_no=$param['goods_no'];
		$goods_name=$param['goods_name'];
		$come_from=$param['come_from'];
		$merchant_code=$param['merchant_code'];
		
		if(!empty($goods_no)){
			$where['goods_no']=array('like','%$goods_no%');
		}
		if(!empty($goods_name)){
			$where['goods_name']=array('like','%$goods_name%');
		}
		if(!empty($come_from)){
			$where['come_from']=$come_from;
		}
		if(!empty($merchant_code)){
			$where['left(goods_no,10)']=array('like','%merchant_code%');//sql截取左边10位即商家编码
		}
		
		if($start_time!='' && $end_time==''){
			$start_time=strtotime($start_time);
			$end_time=strtotime(date('Y-m-d',time()+86400));
        }else if($start_time=='' && $end_time!=''){
			$end_time=strtotime($end_time);
			$start_time=strtotime(date('Y-m-d',time()));
        }else if($start_time=='' && $end_time=='') {
			$start_time=strtotime(date('Y-m-d',time()));
			$end_time=strtotime(date('Y-m-d',time()+86400));
		}else if($start_time!='' && $end_time!='') {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
		}

		$map['add_time']= array(array('gt',0),array('lt',$end_time));
		
		$nums_month=$this->getMonthNum(date('Y-m-d',$start_time),date('Y-m-d',$end_time));
		
		if($end_time-$start_time>=86400){//一天内
			//按小时统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:00:00') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d %H')")->select();//时间列表
		}
		
		if($nums_month<1){//一个月内
			//按天统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")->select();//时间列表
		}
		
		if($nums_month>1 && $nums_month<=12){//一年内
			//按月统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m')")->select();//时间列表
		}
		$count_all=0;
		foreach($list as $k=>$v){
			if(!$k){
				$categories='"'.$v['t_time'].'"';
				$datas=$v['t_count'];
			}else{
				$categories.=',"'.$v['t_time'].'"';
				$datas.=','.$v['t_count'];
			}
			$count_all+=$v['t_count'];
		}
		// dump($datas);
		// dump($categories);
		// echo $m->getlastsql();
		$this->assign('count_all',$count_all);
		$this->assign('categories',$categories);
		$this->assign('datas',$datas);
		$this->assign('list_t',$list);
		$this->display();
    }
	//快递统计
	public function ems(){
		$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));

		$param = I('param.','','trim');
		
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		
		$goods_no=$param['goods_no'];
		$goods_name=$param['goods_name'];
		$come_from=$param['come_from'];
		$merchant_code=$param['merchant_code'];
		
		if(!empty($goods_no)){
			$where['goods_no']=array('like','%$goods_no%');
		}
		if(!empty($goods_name)){
			$where['goods_name']=array('like','%$goods_name%');
		}
		if(!empty($come_from)){
			$where['come_from']=$come_from;
		}
		if(!empty($merchant_code)){
			$where['left(goods_no,10)']=array('like','%merchant_code%');//sql截取左边10位即商家编码
		}
		
		if($start_time!='' && $end_time==''){
			$start_time=strtotime($start_time);
			$end_time=strtotime(date('Y-m-d',time()+86400));
        }else if($start_time=='' && $end_time!=''){
			$end_time=strtotime($end_time);
			$start_time=strtotime(date('Y-m-d',time()));
        }else if($start_time=='' && $end_time=='') {
			$start_time=strtotime(date('Y-m-d',time()));
			$end_time=strtotime(date('Y-m-d',time()+86400));
		}else if($start_time!='' && $end_time!='') {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
		}

		$map['add_time']= array(array('gt',0),array('lt',$end_time));
		
		$nums_month=$this->getMonthNum(date('Y-m-d',$start_time),date('Y-m-d',$end_time));
		
		if($end_time-$start_time>=86400){//一天内
			//按小时统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:00:00') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d %H')")->select();//时间列表
		}
		
		if($nums_month<1){//一个月内
			//按天统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m-%d') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")->select();//时间列表
		}
		
		if($nums_month>1 && $nums_month<=12){//一年内
			//按月统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(add_time,'%Y-%m') t_time")->where($map)->group("FROM_UNIXTIME(add_time,'%Y-%m')")->select();//时间列表
		}
		$count_all=0;
		foreach($list as $k=>$v){
			if(!$k){
				$categories='"'.$v['t_time'].'"';
				$datas=$v['t_count'];
			}else{
				$categories.=',"'.$v['t_time'].'"';
				$datas.=','.$v['t_count'];
			}
			$count_all+=$v['t_count'];
		}
		// dump($datas);
		// dump($categories);
		// echo $m->getlastsql();
		$this->assign('count_all',$count_all);
		$this->assign('categories',$categories);
		$this->assign('datas',$datas);
		$this->assign('list_t',$list);
		$this->display();
    }
}